package com.papper.common.service.sys;

import com.papper.common.bean.sys.SysConfig;
import com.papper.common.cache.CacheConstants;
import com.papper.common.mapper.sys.SysConfigMapper;
import com.papper.common.service.ServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 系统配置业务
 */
@Service("sysConfigService")
public class SysConfigService extends ServiceImpl<SysConfigMapper, SysConfig> {

	@Override
	protected void clearCaches(SysConfig entity) {
		super.clearCaches(entity);
		String model = getModel();
		cache.delete(CacheConstants.Keys(model, entity.getCodeKey()));
		cache.delete(CacheConstants.Keys(model, entity.getCodeKey(), entity.getParentId()));
		cache.delete(CacheConstants.Keys(model, "PA", entity.getId()));
		cache.delete(CacheConstants.Keys(model, "PA", entity.getParentId()));
	}

	/**
	 * 根据名称获取字典信息.
	 *
	 * @param name the name
	 * @return the sys code
	 */
	public SysConfig findByKey(final String name) {
		SysConfig rtn     = null;
		Example   example = new Example(SysConfig.class);
		example.createCriteria().andEqualTo("codeKey", name);
		example.setOrderByClause("id desc limit 1");
		rtn = findOne(example);
		return rtn;
	}

	/**
	 * 根据主键获取字典信息.
	 *
	 * @param key      the key
	 * @param parentId the parent id
	 * @return the sys code
	 */
	public SysConfig findByKey(final String key, final Long parentId) {
		SysConfig rtn     = null;
		Example   example = new Example(SysConfig.class);
		example.createCriteria().andEqualTo("codeKey", key).andEqualTo("parentId", parentId);
		example.setOrderByClause("id desc limit 1");
		rtn = findOne(example);
		return rtn;
	}

	/**
	 * 获取子类字典列表.
	 *
	 * @param parentId the parent id
	 * @return the list
	 */
	public List<SysConfig> findByParentId(final Long parentId) {
		List<SysConfig> rtn     = null;
		Example         example = new Example(SysConfig.class);
		example.createCriteria().andEqualTo("parentId", parentId);
		example.setOrderByClause("code_sort asc");
		rtn = find(example);
		return rtn;
	}

	/**
	 * 获取子类字典列表.
	 *
	 * @param parentKey the parent key
	 * @return the list
	 */
	public List<SysConfig> findChild(String parentKey) {
		SysConfig SysConfig = findByKey(parentKey);
		if (SysConfig == null) {
			return null;
		}
		return findByParentId(SysConfig.getId());
	}

	/**
	 * 根据关键值获取字典信息.
	 *
	 * @param parentKey the parent key
	 * @param key       the key
	 * @return the sys code
	 */
	public SysConfig findByKey(String parentKey, String key) {
		SysConfig SysConfig = findByKey(parentKey);
		if (SysConfig == null) {
			return null;
		}
		SysConfig = findByKey(key, SysConfig.getId());
		return SysConfig;
	}

	/**
	 * 更新系统字典信息.
	 *
	 * @param SysConfig the sys code
	 * @param key       the key
	 * @param value     the value
	 * @param desc      the desc
	 */
	public void modifyKeyValue(SysConfig SysConfig, String key, String value, String desc) {
		if (SysConfig == null) {
			SysConfig add = new SysConfig();
			add.setCodeKey(key);
			add.setCodeValue(value);
			add.setCodeDesc(desc);
			add.setCreateTime(new Date());
			add.setParentId(0);
			add.setDataStatus(0);
			add.setModifyTime(new Date());
			save(add);
		} else {
			SysConfig upd = new SysConfig();
			upd.setId(SysConfig.getId());
			upd.setCodeKey(key);
			upd.setCodeValue(value);
			upd.setModifyTime(new Date());
			updateNoLog(upd);
		}
	}
}